// 新的提交数据(异步)
function do_ajaxSubmit1(fieldName, formName, url) {

	var eurl = encodeURI(url);
	var x = $("#" + formName + "").serializeArray();
	var jsonStr = [];
	$.each(x, function(i, field) {
		if (field.name != 'undefined') {
			if ((field.name).indexOf("2") >= 0) {
				jsonStr.push((field.name).replace('2', '') + ":" + "'"
						+ stripscript(field.value) + "'");
			} else {
				jsonStr.push(field.name + ":" + "'" + stripscript(field.value)
						+ "'");
			}
		}
	});

	$.ajax({
		type : "POST",
		url : eurl,
		data : "json={" + jsonStr + "}",
		dataType : "json",
		error : function(XMLHttpRequest, textStatus, thrownError) {
			var sessionstatus = XMLHttpRequest
					.getResponseHeader("sessionstatus");// 通过XMLHttpRequest取得响应头，sessionstatus，
			if (sessionstatus == "timeout") {
				$.ligerDialog.confirm('Session失效!是否重新登录!', function(yes) {
					if (yes == true) {
						parent.location.reload();// 如果超时就处理 ，指定要跳转的页面
					}
					;
				});
			}
		},
		success : function(data) {
			$.ligerDialog.success(data.msg);
			g.loadData();
			if ((data.id > 0) && (fieldName != ''))
				$.ligerui.get(fieldName).setValue(data.id);
		}
	});
}

/**
 * 新的提交数据(同步), fieldName字段名：
 * 1.保存时传入 2.需要返回时传入
 * 
 */
function do_ajaxSubmit2(fieldName, formName, url, jsonStr) {
	var eurl = encodeURI(url);
	var x = $("#" + formName + "").serializeArray();
	$.each(x, function(i, field) {
		if (field.name != 'undefined') {
			/*if ((field.name).indexOf("2") >= 0) {
				jsonStr.push((field.name).replace('2', '') + ":" + "'"
						+ stripscript(field.value) + "'");
			} else */
			{
				jsonStr.push(field.name + ":" + "'" + stripscript(field.value) + "'");
			}
		}
	});

	$.ajax({
		type : "POST",
		url : eurl,
		data : "json={" + jsonStr + "}",
		dataType : "json",
		async : false,
		error : function(XMLHttpRequest, textStatus, thrownError) {
			// 通过XMLHttpRequest取得响应头，sessionstatus，
			var sessionstatus = XMLHttpRequest
					.getResponseHeader("sessionstatus");
			if (sessionstatus == "timeout") {
				$.ligerDialog.confirm('Session失效!是否重新登录!', function(yes) {
					if (yes == true) {
						parent.location.reload();// 如果超时就处理 ，指定要跳转的页面
					}
					;
				});
			}
			$.ligerDialog.error(thrownError);
		},
		success : function(data) {
			$.ligerDialog.success(data.msg);
			g.loadData();
			if ((data.id > 0) && (fieldName != ''))
				$.ligerui.get(fieldName).setValue(data.id);
		}
	});
}

/**
 * 过滤特殊字符
 * 
 */
function stripscript(s) {
	if(s==null){
		return s;
	}
	var pattern = new RegExp("[\%&'{}]");
	var rs = "";
	for ( var i = 0; i < s.length; i++) {
		rs = rs + s.substr(i, 1).replace(pattern, '');
	}
	return rs;
}
/**
 * 不根据Form提交
 */
var manager = null;
function do_ajaxSubmit(param, url) {
	var eurl = encodeURI(url);
	$.ajax({
		type : "POST",
		url : eurl,
		timeout : 3000,
		async : false,
		data : "json=" + liger.toJSON(param) + "",
		dataType : "json",
		error : function(XMLHttpRequest, textStatus, errorThrown) {
			$.ligerDialog.error("请求错误..." + errorThrown);
		},
		beforeSend : function(XMLHttpRequest) {
			// $.ligerDialog.success("正在执行……");
			manager = $.ligerDialog.waitting('正在执行,请稍候...');
		},
		success : function(data, textStatus) {
			$.ligerDialog.success(data.msg);
			g.loadData();
		},
		complete : handleResponse
	});
}

// 成功返回的绑定函数
function handleResponse(XMLHttpRequest, textStatus) {
	manager.close();
}

// js去空格
function jsTrim(str) {
	return str.replace(/\ /g, "");
}

/**
 * HEYONGJIAN 2014-01-21 去掉字符串左右空格
 */
// 去掉空格
function trim(str) { // 删除左右两端的空格
	return str.replace(/(^\s*)|(\s*$)/g, "");
}
function ltrim(str) { // 删除左边的空格
	return str.replace(/(^\s*)/g, "");
}
function rtrim(str) { // 删除右边的空格
	return str.replace(/(\s*$)/g, "");
}

// 删除LigerUI产生的分页信息
function delPage(data) {
	delete data['__id'];
	delete data['__previd'];
	delete data['__index'];
	delete data['__status'];
	delete data['__nextid'];
	delete data['ROWNUM_'];
	return data;
}
// 格式化数字列
function formatCurrency(num) {
	if (!num)
		return "0.00";
	num = num.toString().replace(/\$|\,/g, '');
	if (isNaN(num))
		num = "0.00";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	cents = num % 100;
	num = Math.floor(num / 100).toString();
	if (cents < 10)
		cents = "0" + cents;
	for ( var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + ','
				+ num.substring(num.length - (4 * i + 3));
	return (((sign) ? '' : '-') + '' + num + '.' + cents);
}

// 前台日期转换
function getFormatDate(date) {
	if (date == "NaN")
		return null;
	var format = "yyyy-MM-dd hh:mm:ss";
	var o = {
		"M+" : date.getMonth() + 1,
		"d+" : date.getDate(),
		"h+" : date.getHours(),
		"m+" : date.getMinutes(),
		"s+" : date.getSeconds(),
		"q+" : Math.floor((date.getMonth() + 3) / 3),
		"S" : date.getMilliseconds()
	};
	if (/(y+)/.test(format)) {
		format = format.replace(RegExp.$1, (date.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	}
	for ( var k in o) {
		if (new RegExp("(" + k + ")").test(format)) {
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
					: ("00" + o[k]).substr(("" + o[k]).length));
		}
	}
	return format;
}

/* heyongjian清空表格 */
function cleanGrid(grid) {
	// 清空表格
	var i = grid.recordNumber;
	while (i > 0) {
		grid.deleteRow(i - 1);
		i--;
	}
}
/* heyongjian根据列名返回列索引 */
function ColumnsIndex(grid,ColumnName) {
	for(var i=0;i<grid.columns.length;i++){
		if(grid.columns[i].columnname == ColumnName){
			return i;
		}
	}
	return -1;
}

/* 获取当月第一天 */
function getCurrFirstDate() {
	var myDate = new Date();
	var year = myDate.getFullYear();
	var month = myDate.getMonth() + 1;
	if (month < 10) {
		month = "0" + month;
	}
	return year + "-" + month + "-" + "01" + " 00:00:00";
}
/* 获取当月最后一天 */
function getCurrLastDate() {
	var myDate = new Date();
	var year = myDate.getFullYear();
	var month = myDate.getMonth() + 1;
	if (month < 10) {
		month = "0" + month;
	}
	myDate = new Date(year, month, 0);
	return year + "-" + month + "-" + myDate.getDate() + " 23:59:59";
}
/* 获取当前日期的前30天 */
function getUpMonthDate() {
	// var now=new Date();
	var newdate = new Date();
	var newtimems = newdate.getTime() - (24 * 60 * 60 * 1000 * 30);
	newdate.setTime(newtimems);
	newdate.setHours("23");
	newdate.setMinutes("59");
	newdate.setSeconds("59");
	// var newdate=newdate.toLocaleString();
	return newdate;
}
// -------------------------------------------------------------------------------
// 结合SHIFT,CTRL,ALT键实现单选或多选
// -------------------------------------------------------------------------------
var KEY = {
	SHIFT : 16,
	CTRL : 17,
	ALT : 18,
	DOWN : 40,
	RIGHT : 39,
	UP : 38,
	LEFT : 37
};

var selectIndexs = {
	firstSelectRowIndex : 0,
	lastSelectRowIndex : 0
};
var inputFlags = {
	isShiftDown : false,
	isCtrlDown : false,
	isAltDown : false
};

function keyPress(event) {// 响应键盘按下事件
	var e = event || window.event;
	var code = e.keyCode | e.which | e.charCode;
	switch (code) {
	case KEY.SHIFT:
		inputFlags.isShiftDown = true;
		break;
	default:
	}
}

function keyRelease(event) { // 响应键盘按键放开的事件
	var e = event || window.event;
	var code = e.keyCode | e.which | e.charCode;
	switch (code) {
	case KEY.SHIFT:
		inputFlags.isShiftDown = false;
		selectIndexs.firstSelectRowIndex = 0;
		break;
	default:
	}
}

/*处理图片大小 img onload时调用**/
function DrawImage(ImgD,width,height){ 
    var image=new Image(); 
    var iwidth = width; 
    var iheight = height; 
    image.src=ImgD.src;
    if(image.width>0 && image.height>0){ 
        flag=true; 
        if(image.width/image.height>= iwidth/iheight){ 
            if(image.width>iwidth){ 
                ImgD.width=iwidth; 
                ImgD.height=(image.height*iwidth)/image.width; 
            } 
            else{ 
                ImgD.width=image.width; 
                ImgD.height=image.height; 
            } 
            ImgD.alt=image.width+"×"+image.height; 
        } 
        else{ 
            if(image.height>iheight){ 
                ImgD.height=iheight; 
                ImgD.width=(image.width*iheight)/image.height; 
            } 
            else{ 
                ImgD.width=image.width; 
                ImgD.height=image.height; 
            } 
            ImgD.alt=image.width+"×"+image.height; 
        } 
    } 
}